<?php

class PhpFox_Mod_Video_ComponentBrowse extends BaseComponent
{
    function process()
    {
		App::checkUserAccess('video', array('full', 'read_only'));

    	$oSrvVideo = &$this->_oModule->getService('Video');
    	$oReq = &App::getRequest();
    	$oUrl = &App::getUrl();

		if ( ($iVidDelete = $oReq->getInt('dvideo')) && App::isStaff(true) )
		{
			$bIsDeleted = $oSrvVideo->deleteVideo($iVidDelete);
		}

    	App::loadClass('Pager');

		$iPageSize = 9;

        if (!$iPageSize)
        {
            $iPage = 0;
            $oUrl->clearParams('page');
        }
        else
        {
            $iPage = ($this->getParam('page') ? $this->getParam('page')  : $oReq->getInt('page', 1));
        }

        if ( ($iRemoveReport = $oReq->getInt('rmreport')) && App::isStaff() )
        {
			$this->oDatabase->update(App::getT('videos'), array('reported' => 0), "vid_id = '". $iRemoveReport ."'");
        }

		$sQ = "";
		$sOrder = " v.vid_time DESC ";

        if ( !$oReq->get('search') && ($sTags = $oReq->get('tag')) )
        {
			$oUrl->setParam('tag', strip_tags($sTags));
        	$sQ .= " AND v.vid_tags LIKE '%". $this->oDatabase->escape(strip_tags($sTags)) ."%' ";
        }

        if ( $iListId = $oReq->getInt('listid') )
        {
			$sQ .= " AND v.vid_list_id = '". $iListId ."' ";
        }

        if ( $sSearchQuery = $oReq->get('search') )
        {
        	$oUrl->setParam('search', strip_tags($sSearchQuery));
			$sQ .= " AND (v.vid_title LIKE '%". $this->oDatabase->escape(strip_tags($sSearchQuery)) ."%' OR v.vid_info LIKE '%". $this->oDatabase->escape(strip_tags($sSearchQuery)) ."%' OR v.vid_tags LIKE '%". $this->oDatabase->escape(strip_tags($sSearchQuery)) ."%') ";
        }

		if ( ($sReported = $oReq->get('reported')) && App::isStaff() )
		{
			$sQ .= " AND v.reported != '' ";
		}

        if ( $sType = $oReq->get('type') )
        {
        	switch($sType)
        	{
     			case 'featured':
     			$sQ .= " AND v.featured = '1' ";
     			break;
     			case 'most_viewed':
     			$sOrder = " v.vid_total DESC ";
     			break;
     			case 'most_comments':
				$sOrder = " v.comments DESC ";
     			break;
     			case 'rating':
     			$sOrder = " v.vid_rating DESC ";
     			break;
        	}
        }

        if ( $iSearchUserId = $this->getParam('userid') )
        {
			$sQ = " AND v.vid_userid = '". $iSearchUserId ."' ";
        }

        $iTotal = $this->oDatabase->getRow("SELECT COUNT(*) AS count FROM ". App::getT('videos') ." AS v
        	WHERE 1 ". $sQ ."");
		$iCnt = $iTotal['count'];

		$iOffset = $this->oDatabase->_getPageNum($iPage, $iPageSize, $iCnt);

   		$aItems = $this->oDatabase->getRows("
   			SELECT 
   				v.vid_id, v.vid_title, v.vid_url, v.stream_id, v.vid_time, v.vid_total, v.vid_rating, v.vid_rating_count, v.featured, v.duration, v.reported, u.user 
   			FROM ". App::getT('videos') ." AS v
   				JOIN ". App::getT('user') ." AS u ON(v.vid_userid = u.id)
   			WHERE 1 ". $sQ ."
   			ORDER BY ". $sOrder ." LIMIT ". $iOffset .",". $iPageSize ."
   		");

		$oSrvVideo = &App::getModuleService('Video', 'Video');
		$aContent = array();
		foreach($aItems as $aItem)
		{
			if ( App::isStaff() )
			{
				$i = 0;
				$aWarnings = array();
				foreach(App::getMessages('Mod_Video.warning') as $aWarning)
				{
					$i++;
					if ( $aItem['reported'] == $i )
					{
						$aItem['reported'] = $aWarning;
						break;
					}
				}
			}

			$aItem['stars'] = $oSrvVideo->prepareStars($aItem['vid_rating']);
			$aContent[] = $aItem;
		}

		$oPager = &new Pager($iCnt, $iPage, $iPageSize);
		$iFrom = $oPager->getOffset($iPage, $iPageSize);
		$iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

		$aBrowseLinks = array
		(
			'recent',
			'most_viewed',
			'rating',
			'most_comments',
			'featured',

		);

		$aCategories = $this->oDatabase->getRows("SELECT list_id, list_name FROM ". App::getT('videos_list') ." ORDER BY list_name");

		if ( isset($iListId) )
		{
			foreach($aCategories as $aCategory)
			{
				if ( $iListId == $aCategory['list_id'] )
				{
					$sCategoryTitle = $aCategory['list_name'];
					break;
				}
			}
		}

		$this->oTpl->assign(array
		(
			'iListId' => (isset($iListId) ? $iListId : ''),
			'sCategoryTitle' => (isset($sCategoryTitle) ? $sCategoryTitle : ''),
			'aCategories' => $aCategories,
			'sType' => (isset($sType) ? $sType : ''),
			'aBrowseLinks' => $aBrowseLinks,
			'sGImageUrl' => App::getParam('video.image.url'),
			'aItems' => $aContent,
        	'iCnt'             => $iCnt,
        	'iFrom'            => ($iCnt) ? ($iFrom + 1) : 0,
			'aPaging'          => ($iCnt) ? $oPager->getInfo($oUrl) : '',
        	'iTo'              => $iTo,
        	'bIsProfile' => (!empty($iSearchUserId) ? true : false),
        	'iHotVideo' => App::getParam('vid_hot_video'),
        	'bIsDeleted' => (isset($bIsDeleted) ? true : false),
        	'bIsFeatured' => (isset($bIsFeatured) ? true : false),
        	'bIsRemovedFeatured' => (isset($bIsRemovedFeatured) ? true : false)
		));

		return $this->oTpl->fetch('Browse.html');
    }

}

?>